package com.local.user.service.impl;

import com.local.common.util.PageResultUtil;
import com.local.user.mapper.UserMapper;
import com.local.user.pojo.User;
import com.local.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 查询用户列表，支持名称搜索
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param userName
     * @param isDeleted
     * @param lockedFlag
     * @return
     */
    @Override
    public PageResultUtil getUserList(Integer page, Integer limit, String sort, String order, String userName, Integer isDeleted, Integer lockedFlag) {
        Integer start = (page - 1) * limit;
        List<User> userList = userMapper.getUserList(start, limit, sort, order, userName, isDeleted, lockedFlag);
        Integer totalUsers = userMapper.getTotalUsers(userName, isDeleted);
        PageResultUtil<User> userPageResultUtil = new PageResultUtil<>(userList, totalUsers, limit, page);

        return userPageResultUtil;
    }


    /**
     * 锁定/解锁用户状态
     * @param ids
     * @param lockedFlag
     * @return
     */
    @Override
    public Integer updateLockedFlag(Integer[] ids, Integer lockedFlag) {
        return userMapper.updateLockedFlag(ids, lockedFlag);
    }


    /**
     * 通过用户名和密码查询用户，用于前台登录
     * @param loginName
     * @param password
     * @return
     */
    @Override
    public User getByLoginNamePassword(String loginName, String password) {
        return userMapper.getByLoginNamePassword(loginName, password);
    }


    /**
     * 注册新用户
     * @param user
     * @return
     */
    @Override
    public Integer save(User user) {
        User userByLoginName = userMapper.getByLoginName(user.getLoginName());
        if (Objects.isNull(userByLoginName)) {
            return userMapper.save(user);
        } else {
            //用户名已被占用
            return -1;
        }
    }

    /**
     * 更新用户地址
     * @param user
     * @return
     */
    public Integer updateAddress(User user) {
        Integer result = userMapper.updateAddress(user);
        return result;
    }


//    /**
//     * 更新用户信息
//     * @param user
//     * @param session
//     * @return
//     */
//    @Override
//    public Integer update(User user, HttpSession session) {
//        Integer updateResult = userMapper.updateInfo(user);
//        if (updateResult > 0) {
//            //修改成功之后，同时修改session中的user信息
//            User userSession = (User) session.getAttribute(Constants.MALL_USER_SESSION_KEY);
//            userSession.setNickName(user.getNickName());
//            userSession.setAddress(user.getAddress());
//            userSession.setIntroduceSign(user.getIntroduceSign());
//
//            session.removeAttribute(Constants.MALL_USER_SESSION_KEY);
//            session.setAttribute(Constants.MALL_USER_SESSION_KEY, userSession);
//        }
//        return updateResult;
//    }


}
